import java.util.*;
import local.util.*;

public class DetailDataCollecter {

    public static void main( String args[] )
    {

       if (args.length != 2) {
	   System.out.println("----------------------------------------");
	   System.out.println("Collect Data:          [1][Tick]");
	   System.out.println("Testing:               [2][Tick]");	
	   System.out.println("Other:                 [3][Tick]");	
	   System.out.println("----------------------------------------");
	   System.exit(0);
       }

       String choice    = args[0];
       String tick_name = args[1];
       String try_url   = "";

	try_url = "https://www.bigcharts.com/custom/datek-com/datek-rt2.asp?osymb=&symb="+args[1]+
	    "&time=1dy&freq=1mi&compidx=aaaaa%7E0&comp=&type=2&ma=0&maval=9&uf=0&lf=1&"+
	    "lf2=0&lf3=0&sid=&x=42&y=6";

       if (choice.equals("1")) {
	   CollectData cl = new CollectData();
	   cl.setTickName(tick_name);
	   DataCollecter thread = new DataCollecter(cl);
	   thread.start();
       }
       else if (choice.equals("2")) {
	   TestCollectData tcl = new TestCollectData();
	   tcl.setTickName(tick_name);
	   DataCollecter thread = new DataCollecter(tcl);
	   thread.start();
       }
       else if (choice.equals("3")) {
	   RealDataCollecter rdc = new RealDataCollecter ();
	   rdc.setTick(tick_name);
	   rdc.setUrl(try_url);
	   DataCollecter thread = new DataCollecter(rdc);
	   thread.setSleepTime (1000*6); // 1 seconds 
   	   thread.start();
       }
   }
}

class DataCollecter extends Thread {
    private CollectData cldata = null;
    private TestCollectData tcldata = null;
    private RealDataCollecter rdata = null;
    private int sleepTime = 10000; // 10 seconds

    public void setSleepTime (int sleep_time) {
	sleepTime = sleep_time;
    }

    public DataCollecter( TestCollectData cd )
    {
	super( "DataCollect" );
	tcldata = cd;
    }

    public DataCollecter( CollectData cd )
    {
	super( "DataCollect" );
	cldata = cd;
    }

   public DataCollecter( RealDataCollecter cd )
    {
	super( "RealDataCollecter" );
	rdata = cd;
    }

   public void run()
   {
       for (int i=0; i<10; i++) {
	   Calendar c = Calendar.getInstance();
	      
	   //System.out.println("HOUR:"+c.get(Calendar.HOUR));
	   //System.out.println("MINUTE:"+c.get(Calendar.MINUTE));
	   //System.out.println("SECOND:"+c.get(Calendar.SECOND));
	   /*
	   if (((c.get(Calendar.HOUR) >= 8) && (c.get(Calendar.AM_PM)!=1)) ||
	       ((c.get(Calendar.HOUR) <= 4) && (c.get(Calendar.AM_PM)==1))) {
	       //System.out.println("Sleep time has been changed to 1000*60");
	       sleepTime = 1000*60*5; //1000*60
	   }
	   */
	   if (cldata != null) {
	       System.out.println("Runing data collection...");
	       cldata.loadData();
	   }

	   if (tcldata != null) {
	       System.out.println("Runing test...");
	       tcldata.loadData();
	   }

	   if (rdata != null) {
	       //System.out.println(MyDateAndTime.getCurrentTime(24));
	       rdata.loadData();
	   }

	   try {
	       Thread.sleep(sleepTime);
	   }
	   catch ( InterruptedException exception ) {
	       System.err.println( exception.toString() );
	       rdata.insertIntoDataBase();
	   }
	   /*
	   if ((c.get(Calendar.HOUR)==4 && c.get(Calendar.AM_PM)==1)||c.get(Calendar.HOUR) == 16) {
	       rdata.insertIntoDataBase();
	   }
	   */
       }
   }
}

class CollectData {

    static String lastVolume="0";
    static String symbol = "";

    public void setTickName(String tick) {
	symbol = tick;
    }

    public void loadData () {

	String query_head = "INSERT INTO tibx_record VALUES (";
	Hashtable dHashTable = new Hashtable();
	int time_h =0, time_m = 0;
	String thisVolume = "0";
	String query_str ="";
	Calendar c = Calendar.getInstance();

	GetTodaysData dc = new GetTodaysData();
	dc.setUrl(symbol);     
	dc.loadData();
	dHashTable = dc.getTodaysDataHashTable(symbol);
	thisVolume = ((String)dHashTable.get("Volume")).replaceAll(",","");

	if (c.get(Calendar.AM_PM)==1) { //PM
	    time_h = c.get(Calendar.HOUR)+12;
	}
	else {
	    time_h = c.get(Calendar.HOUR);
	}

	time_m = c.get(Calendar.MINUTE);

	if(!(thisVolume.equals(lastVolume))) {  
	    DataBaseInsert db_insert = new DataBaseInsert();
	    //dc.printData(dHashTable);
	    //System.out.print(dHashTable.toString());
	    query_str = query_head+"'"+(c.get(Calendar.MONTH)+1)+"/"+c.get(Calendar.DATE)+"',"+
		        "'"+time_h+":"+time_m+"',"+
		        "'"+dHashTable.get("SYMBOL")+"',"+
		        "'"+dHashTable.get("Last")+"',"+"'"+ dHashTable.get("Change")+"',"+
		        "'"+dHashTable.get("High")+"',"+"'"+ dHashTable.get("Low")+"',"+
		        "'"+dHashTable.get("Volume")+"',"+"'"+ dHashTable.get("Prev. Close")+"');";
	    System.out.println(query_str);
	    db_insert.setQueryString(query_str);

	    do {
		db_insert.setQueryString(query_str);
	    } while (db_insert.insertData()!=1);

	    lastVolume = ((String)dHashTable.get("Volume")).replaceAll(",","");
	}
    }
}

class TestCollectData {

    static String symbol = "";
    public void setTickName(String tick) {
	symbol = tick;
    }

    public void loadData () {
	System.out.println("This is a test! input tick ="+symbol);
    }
}


